<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <h2 class="title">图书管理系统 - 登录</h2>
      </template>
      <el-form :model="form" ref="formRef" label-width="100px" label-position="top" class="login-form">
        <el-form-item label="角色" prop="userType">
          <el-radio-group v-model="form.userType" class="radio-group">
            <el-radio label="admin">管理员</el-radio>
            <el-radio label="book_admin">图书管理员</el-radio>
            <el-radio label="user">普通用户</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名或借阅卡号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
              v-model="form.password"
              type="password"
              @keyup.enter="handleLogin"
              placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="primary" @click="handleLogin">登录</el-button>
            <el-button type="success" @click="navigateToRegister">去注册</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>



<script setup>
import {ref} from 'vue';
import {useRouter} from 'vue-router';
import {ElMessage} from 'element-plus';
import http from '../utils/axios'; // 使用封装好的 Axios 实例
import { useStore } from 'vuex';

// 创建响应式数据
const form = ref({
  userType: '',
  username: '',
  password: '',
});

const router = useRouter();
const store = useStore(); // 导入并创建 store 实例


// 登录处理
const handleLogin = async () => {
  if (!form.value.userType) {
    ElMessage.error('请选择角色');
    return;
  }
  try {
    let {userType, username, password} = form.value;
    const response = await http.post('/login', {
      userType,
      username,
      password,
    });
    if (response && response.status) {
      ElMessage.success( '登录成功');
      // 保存角色信息到 localStorage
      localStorage.setItem('username', response.data.username);
      localStorage.setItem('userType', userType);
      if (userType === 'user'){
        localStorage.setItem('cardNumber', response.data.cardNumber);
      }
      username = response.data.username;
      await store.dispatch('updateUser', {username, userType});

      // 根据角色跳转到对应的页面
      if (userType === 'admin') {
        await router.push('/admin');
      } else if (userType === 'book_admin') {
        await router.push('/book-admin');
      } else {
        await router.push('/user-dashboard');
      }
    } else {
      const errorMessage = response ? response.data || '登录失败' : '登录失败';

      // 根据不同的错误响应处理
      if (response && response.code === 401) {
        ElMessage.error('用户名或密码错误');
      } else if (response && response.code === 404) {
        ElMessage.error('请求未找到');
      } else if (response && response.code === 400) {
        ElMessage.error('Bad Request');
      } else {
        ElMessage.error(errorMessage);
      }
    }
  } catch (error) {
    console.error('请求错误:', error);
    if (error.response && error.response.data && error.response.data) {
      ElMessage.error(error.response.data);
    } else {
      ElMessage.error('网络或服务器错误，请稍后再试');
    }
  }
};

// 注册页面跳转
const navigateToRegister = () => {
  router.push('/register');
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f4f6f9;
  background-image: url('../assets/images/background.jpg'); /* 添加背景图片 */
  background-size: cover; /* 使背景图片覆盖整个容器 */
  background-position: center; /* 将背景图片居中 */
  background-repeat: no-repeat; /* 防止背景图片重复 */
  min-height: 100vh; /* 确保背景图片至少覆盖整个视口高度 */
}

/* 卡片样式 */
.login-card {
  width: 420px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
}

/* 标题样式 */
.title {
  text-align: center;
  color: #409EFF;
  font-size: 26px;
  letter-spacing: 2px;
  margin: 15px 0;
}

/* 表单统一宽度 */
.login-form .el-form-item__content {
  width: 100%;
}

/* 表单项间距 */
.el-form-item {
  margin-bottom: 20px;
}

/* 输入框样式 */
.el-input__inner {
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: box-shadow 0.3s;
}

.el-input__inner:focus {
  border-color: #409EFF;
  box-shadow: 0 0 8px rgba(64, 158, 255, 0.3);
}

/* 单选框组样式 */
.radio-group .el-radio {
  margin-right: 20px;
}

/* 按钮组样式 */
.el-button-group .el-button {
  padding: 12px 77px;
}

.el-button-group .el-button--text {
  color: #409EFF;
}

.el-button-group .el-button--text:hover {
  color: #66b1ff;
}
</style>


